package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.JyGoods;
import com.jy.adscm.operation.base.mapper.mysql.JyGoodsMapper;
import com.jy.adscm.operation.mscyb.bean.JqbzPartsassemblydetail;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.bean.JqylMaterialdetail;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzPartsassemblydetailMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialdetailMapper;
import com.jy.adscm.operation.mscyb.service.JqbzPartsassemblydetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-26
 */
@Service
public class JqbzPartsassemblydetailServiceImpl extends ServiceImpl<JqbzPartsassemblydetailMapper, JqbzPartsassemblydetail> implements JqbzPartsassemblydetailService {
    @Autowired
    JyGoodsMapper jyGoodsMapper;
    @Autowired
    JqylMaterialMapper jqylMaterialMapper;
    @Autowired
    JqylMaterialdetailMapper jqylMaterialdetailMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Boolean updateJqbzPartsassemblydetail(JqbzPartsassemblydetail jqbzPartsassemblydetail) {
        boolean result = super.updateById(jqbzPartsassemblydetail);
        return result;
    }

    @Override
    public List<JqbzPartsassemblydetail> selectByTid(String tid) {
        Wrapper<JqbzPartsassemblydetail> wrapper =new EntityWrapper<>();
        wrapper.eq("TID",tid);
        List<JqbzPartsassemblydetail> JqbzPartsassemblydetails = super.selectList(wrapper);
        for (JqbzPartsassemblydetail jqbzPartsassemblydetail : JqbzPartsassemblydetails) {
             if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassemblydetail.getUserperson(),null)){
                 JyUser jyUser = jyUserMapper.selectById(jqbzPartsassemblydetail.getUserperson());
                 jqbzPartsassemblydetail.setUserpersonName(jyUser.getName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassemblydetail.getPartsrealname(),null)){
                 JyGoods jyGoods = jyGoodsMapper.selectById(jqbzPartsassemblydetail.getPartsrealname());
                 jqbzPartsassemblydetail.setPartsrealnameName(jyGoods.getName());
             }
        }
        return JqbzPartsassemblydetails;
    }
   @Transactional
    @Override
    public boolean insertJqbzPartsassemblydetail(JqbzPartsassemblydetail jqbzPartsassemblydetail, Token token) throws AdscmSystemException {
         //根据查询的原料号
       if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassemblydetail.getPartsname(),null)){
           JqylMaterial jqylMaterial = jqylMaterialMapper.selectById(jqbzPartsassemblydetail.getPartsname());
          //进行校验
           if (!AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial,null)){
               throw  new AdscmSystemException("没找到任务书信息");
           }
           if (!"1".equals(jqylMaterial.getIstaskbegin())){
               throw  new AdscmSystemException("物料未入库，不能添加出库信息");
           }
           if (jqylMaterial.getRemainingnums()==0){
               throw  new AdscmSystemException("物料已经用完");
           }
           if (jqylMaterial.getRemainingnums()<jqbzPartsassemblydetail.getNums()){
               throw new AdscmSystemException("使用量超过了剩余量");
           }
           //减去任务书中的剩余数量
           DecimalFormat df=new DecimalFormat("#0.0000");
           String format = df.format(jqylMaterial.getRemainingnums()-jqbzPartsassemblydetail.getNums());
           jqylMaterial.setRemainingnums( Float.valueOf(String.valueOf(format)) );
           jqylMaterialMapper.updateById(jqylMaterial);
           //根据任务书中的物料名称获取到物料的编码
           JyGoods jyGoods = jyGoodsMapper.selectById(jqylMaterial.getResourcename());
           //物料编码
           if (AdscmBaseUtils.getInstance().isNotEmpty(jyGoods.getProductcode(),null)){
               String productcode = jyGoods.getProductcode();
               if (jqbzPartsassemblydetail.getNums()>1){
                   throw  new AdscmSystemException("添加数量大于1，不能生成对应条码");
               }
               String yearMonth = new SimpleDateFormat("yyMM").format(new Date());
               Wrapper<JqylMaterialdetail>  wrapper=new EntityWrapper<>();
               wrapper.like("CODE", productcode + yearMonth, SqlLike.RIGHT);
               wrapper.orderBy("CODE",false);
               List<JqylMaterialdetail> jqylMaterialdetails = jqylMaterialdetailMapper.selectList(wrapper);
               String code="";
               if (jqylMaterialdetails.size()>0){
                   JqylMaterialdetail oldjqylMaterialdetail = jqylMaterialdetails.get(0);
                   String oldCode = oldjqylMaterialdetail.getCode();
                   Integer temp = Integer.valueOf(oldCode.substring(oldCode.length()-2, oldCode.length())) ;
                   temp=temp+1;
                   String nums = new DecimalFormat("00").format(temp);
                   code=productcode+yearMonth+nums;
               }else{
                   code=productcode+yearMonth+"00";
               }
               code.trim();
               jqbzPartsassemblydetail.setPartscode(code);
           }
           //部件使用信息
           jqbzPartsassemblydetail.setCreatetime(new Date());
           jqbzPartsassemblydetail.setPartsbatch(jqylMaterial.getBatchnums());
           jqbzPartsassemblydetail.setPartsrealname(jqylMaterial.getResourcename());
            jqbzPartsassemblydetail.setPartstype(jqylMaterial.getTypenums());
            jqbzPartsassemblydetail.setUnit(jqylMaterial.getUnit());
            jqbzPartsassemblydetail.setUserperson(token.getUser().getId().toString());
           //添加原料库中的出库记录
           JqylMaterialdetail  jqylMaterialdetail=new JqylMaterialdetail();
           jqylMaterialdetail.setName(jqylMaterial.getResourcename());
           jqylMaterialdetail.setTypenums(jqylMaterial.getTypenums());
           jqylMaterialdetail.setUnit(jqylMaterial.getUnit());
           jqylMaterialdetail.setBatchnums(jqylMaterial.getBatchnums());
           jqylMaterialdetail.setCode(jqbzPartsassemblydetail.getPartscode());
           jqylMaterialdetail.setOutstorageaddtime(new Date());
           jqylMaterialdetail.setUseperson(token.getUser().getId().toString());
           jqylMaterialdetail.setIsrepair(0);
           jqylMaterialdetail.setUsenums(jqbzPartsassemblydetail.getNums());
           jqylMaterialdetail.setMaterialstate(jqbzPartsassemblydetail.getMaterialstate());
           jqylMaterialdetail.setPid(jqbzPartsassemblydetail.getTid());
           jqylMaterialdetail.setOutstoragetime(jqbzPartsassemblydetail.getUsetime());
           jqylMaterialdetail.setTid(jqylMaterial.getTid().toString());
           jqylMaterialdetailMapper.insert(jqylMaterialdetail);
       }
       boolean insert = super.insert(jqbzPartsassemblydetail);
       return insert;
    }

    @Override
    public List<JqylMaterial> queryMaterial() throws AdscmSystemException {
        Wrapper<JqylMaterial> wrapper =new EntityWrapper<>();
        wrapper.setSqlSelect("TID","RESOURCENAME");
        wrapper.eq("ISTASKBEGIN",1);
        wrapper.eq("ISTASKEND",0);
        List<JqylMaterial> jqylMaterials = jqylMaterialMapper.selectList(wrapper);
        for (JqylMaterial jqylMaterial : jqylMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getResourcename(),null)){
                JyGoods jyGoods = jyGoodsMapper.selectById(jqylMaterial.getResourcename());
                jqylMaterial.setResourcenameName(jyGoods.getName());
            }
        }
        return jqylMaterials;
    }
}
